package com.wonderful.compose.booking

import android.content.Context
import android.widget.Toast
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Build
import androidx.compose.material.icons.filled.KeyboardArrowRight
import androidx.compose.material.icons.filled.ShoppingCart
import androidx.compose.material.icons.rounded.Star
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.rememberAsyncImagePainter
import com.wonderful.compose.ui.theme.BookingPrimary

/**
 * Desc:
 * @Author: wonderful
 * @Time: 2024/1/29 15:14
 */

data class House(
    val name: String,
    val picUrl: String,
    val location: String,
    val rating: Int,
    val features: List<HouseFeature>
)

sealed class HouseFeature(val icon: ImageVector, val text: String) {
    class Bedrooms(count: Int) : HouseFeature(Icons.Filled.ShoppingCart, "$count Bedroom")
    object WiFi : HouseFeature(Icons.Default.Build, "WiFi")
}

val houseItems = listOf(
    House(
        "The Sile House",
        "https://cdn.pixabay.com/photo/2017/09/09/18/25/living-room-2732939_1280.jpg",
        "Mojolaban, Solo",
        3,
        listOf(HouseFeature.Bedrooms(2), HouseFeature.WiFi)
    ),
    House(
        "Matraville Residence",
        "https://cdn.pixabay.com/photo/2017/09/09/18/25/living-room-2732939_1280.jpg",
        "Mojolaban, Solo",
        4,
        listOf(HouseFeature.Bedrooms(3), HouseFeature.WiFi)
    )
)

@Preview(showBackground = false)
@Composable
fun HouseList() {
    LazyRow(
        contentPadding = PaddingValues(16.dp),
        horizontalArrangement = Arrangement.spacedBy(12.dp)
    ) {
        items(houseItems) {
            HouseCard(house = it)
        }
    }
}

@Composable
fun HouseCard(house: House) {
    Surface(shape = MaterialTheme.shapes.large, modifier = Modifier.width(300.dp)) {
        Column(Modifier.padding(16.dp)) {
            HouseImage(house = house)
            Spacer(modifier = Modifier.height(16.dp))
            HouseInfo(house = house)
            Spacer(modifier = Modifier.height(12.dp))
            HouseFeatureList(house = house)
        }
    }
}

@Composable
fun HouseImage(house: House) {
    Image(
        painter = rememberAsyncImagePainter(
            model = house.picUrl,
            contentScale = ContentScale.Crop
        ),
        contentDescription = null,
        modifier = Modifier
            .fillMaxWidth()
            .aspectRatio(10 / 9f)
            .clip(MaterialTheme.shapes.medium)
    )
}

@Composable
fun HouseInfo(modifier: Modifier = Modifier, house: House, horizontal: Boolean = true) {
    val content: @Composable (Modifier) -> Unit = {
        Column(modifier = it) {
            Text(
                text = house.name,
                fontWeight = FontWeight.SemiBold,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis
            )
            Text(
                text = house.location,
                style = MaterialTheme.typography.titleMedium.copy(color = Color.LightGray)
            )

        }
        Spacer(modifier = Modifier.width(8.dp))
        Rating(value = house.rating)
    }
    if (horizontal) {
        Row(modifier = modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
            content(Modifier.weight(0.1f))
        }
    } else {
        Column(modifier = modifier.fillMaxWidth()) {
            content(Modifier)
        }
    }
}

@Composable
fun HouseFeatureList(house: House) {
    val context = LocalContext.current
    Row(Modifier.height(32.dp)) {
        house.features.forEach {
            HouseFeature(houseFeature = it)
            Spacer(modifier = Modifier.width(8.dp))
        }

        Surface(
            color = BookingPrimary,
            shape = MaterialTheme.shapes.small,
            modifier = Modifier
                .fillMaxHeight()
                .aspectRatio(1f)
                .clickable {
                    Toast
                        .makeText(context, "More features click", Toast.LENGTH_SHORT)
                        .show()
                }
        ) {
            Icon(
                imageVector = Icons.Default.KeyboardArrowRight,
                contentDescription = "More Features",
                modifier = Modifier.padding(4.dp),
                tint = MaterialTheme.colorScheme.onPrimary
            )
        }
    }
}

@Composable
fun HouseFeature(houseFeature: HouseFeature) {
    Row(
        modifier = Modifier
            .fillMaxHeight()
            .clip(MaterialTheme.shapes.small)
            .background(Color.LightGray.copy(alpha = 0.05f))
            .border(1.dp, Color.LightGray, MaterialTheme.shapes.small)
            .padding(vertical = 4.dp, horizontal = 12.dp)
    ) {
        Icon(
            imageVector = houseFeature.icon,
            contentDescription = houseFeature.text,
            tint = BookingPrimary,
            modifier = Modifier
                .size(18.dp)
                .align(Alignment.CenterVertically)
        )
        Spacer(modifier = Modifier.width(8.dp))
        Text(
            text = houseFeature.text,
            modifier = Modifier.align(Alignment.CenterVertically),
            fontSize = 12.sp,
            fontWeight = FontWeight.Bold
        )
    }
}

@Composable
fun Rating(value: Int, modifier: Modifier = Modifier) {
    val starColor = Color(0xFFFFA235)
    Row(modifier) {
        repeat(5) {
            Icon(
                imageVector = Icons.Rounded.Star,
                contentDescription = null,
                tint = starColor,
                modifier = Modifier
                    .requiredSize(22.dp)
                    .alpha(if ((it + 1) > value) 0.3f else 1f)
            )
        }
    }
}

@Composable
fun PopularHouse() {
    val house = houseItems[1]
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(horizontal = 16.dp)
    ) {
        Text(
            text = "Popular House",
            style = MaterialTheme.typography.titleSmall,
            fontWeight = FontWeight.SemiBold
        )
        Spacer(modifier = Modifier.height(16.dp))
        Surface(shape = MaterialTheme.shapes.large, modifier = Modifier.fillMaxWidth()) {
            Row(modifier = Modifier.padding(16.dp)) {
                Image(
                    painter = rememberAsyncImagePainter(
                        model = house.picUrl,
                        contentScale = ContentScale.Crop
                    ),
                    contentDescription = null,
                    modifier = Modifier
                        .size(96.dp)
                        .clip(MaterialTheme.shapes.medium)
                )
                Spacer(modifier = Modifier.width(22.dp))
                HouseInfo(
                    house = house,
                    horizontal = false,
                    modifier = Modifier.align(Alignment.CenterVertically)
                )
            }
        }
    }
}